<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '98%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,  //创建chartInstance对象
      currentPage:1,//当前页数*
      totalPage:0,//总共页数*

    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {//生命周期
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ powerData, heatingPowerData} = {}) {
      this.chart.setOption({
        title:{
          text:'净购入电力、热力对应的排放年度统计',
          left: 50
        },
        dataZoom: [ // 控制区域缩放效果的实现*
          {
            type: 'inside', // 缩放的类型  slide代表滑块  inside代表依靠鼠标滚轮
            xAxisIndex: 0
          },
          {
            type: 'slider',
            yAxisIndex: 0,
          }
        ],
        xAxis: {
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月','八月','九月','十月','十一月','十二月'],
          boundaryGap: false, //使得一月数据紧挨边缘
          axisTick: {
            show: false
          }
        },
        grid: { //直角坐标系坐标轴的配置
          left: 10,
          right: 37,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {  //工具提示
          trigger: 'axis', //当鼠标移入坐标轴进行工具提示显示
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10],
          formatter: function (params) {
            var relVal = params[0].name
            for (var i = 0, l = params.length; i < l; i++) {
              relVal += '<br/>' + params[i].marker + params[i].seriesName + ' : ' + params[i].value + '(tCO\u2082)'
            }
            return relVal
          }
        },
        toolbox:{
          feature:{
            saveAsImage:{},//导出图片*
            dataView:{},//数据视图*
            restore:{},//重置*
            dataZoom:{},//区域缩放*
            magicType:{
              type:['line','bar']
            }
          }
        },
        yAxis: {
          axisTick: {
            show: false
          },
          scale:true   //脱离0值比例*
        },
        legend: {
          data: ['电力', '热力']
        },
        series: [{
          name: '电力', itemStyle: {
            normal: {
              color: 'rgba(22,242,217,1)',
              lineStyle: {
                color: 'rgba(22,242,217,1)',
                width: 2
              },
              areaStyle: {
                color: new this.$echarts.graphic.LinearGradient(0,0,0,1,[   //颜色渐变
                  {
                    offset:0,
                    color:'rgba(22,242,217,0.5)'},//0%的颜色值
                  {
                    offset:1,
                    color:'rgba(22,242,217,0)'}//100%的颜色值
                ]),
              }
            }
          },
          smooth: true,
          type: 'line',
          data: powerData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut',
          stack: 'all', // 堆叠图的设置*
        },
        {
          name: '热力',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: 'rgba(44,110,255,1)',
              lineStyle: {
                color: 'rgba(44,110,255,1)',
                width: 2
              },
              areaStyle: {
                color: new this.$echarts.graphic.LinearGradient(0,0,0,1,[   //颜色渐变
                  {
                    offset:0,
                    color:'rgba(44,110,255,0.5)'},//0%的颜色值
                  {
                    offset:1,
                    color:'rgba(44,110,255,0)'}//100%的颜色值
                ])
              }
            }
          },
          data: heatingPowerData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut',
          stack: 'all', // 堆叠图的设置*
        },
        ]
      })
    }
  }
}
</script>
